package com.zl_huang.example.dao;

import java.util.List;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.zl_huang.example.model.User;
@Repository
public class UserDaoHibernateImpl extends BaseHibernateDAO<User> implements IUserDaoHibernate {
	

	@Override
	@Transactional
	public Integer createUser(User user) throws Exception {
		Session session = getSession();
		session.save(user);
		return 1;
	}

	@Override
	@Transactional
	public Integer editUser(User user) throws Exception {
		Session session = getSession();
		session.update(user);
		return 1;
	}

	@Override
	@Transactional
	public Integer deleteUser(String user_id) throws Exception {
		Session session = getSession();
		User user = (User) session.get(User.class, user_id);
		if(user != null){
			session.delete(user);
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	@Transactional
	public List<User> findAllUsers() throws Exception {
		Session session = getSession();
		String hql = "from User";
		@SuppressWarnings("unchecked")
		List<User> userList = session.createQuery(hql).list();
		return userList;
	}

	@Override
	@Transactional
	public User getUserById(String user_id) throws Exception {
		Session session = getSession();
		return (User) session.get(User.class, user_id);
	}

}
